package com.goldgov.kduck.endpoint;

import com.goldgov.kduck.event.RemoteRefreshRouteEvent;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.oauth2.provider.endpoint.FrameworkEndpoint;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 自定义网关监控端点
 *
 * @author guor
 */
@RestControllerEndpoint(
        id = "open"
)
@RefreshScope
@FrameworkEndpoint
public class ApiEndpoint implements InitializingBean {
    @Autowired
    ApplicationEventPublisher publisher;

    /**
     * 远程刷新网关
     * /actuator/open/refresh
     *
     */
    @PostMapping("/refresh")
    @ResponseBody
    public String busRefreshWithDestination() {
        publisher.publishEvent(new RemoteRefreshRouteEvent(this));
        return "OK";
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}
